﻿// 正则
/*
      if(!memoRemark.GetText().match(/\S/))
          alert("必须填写审批备注！");
*/
jQuery.validator.addMethod("regExp", function (value, element,p) {
    var exp = new RegExp(p);//C#中调用匹配一个9位数字如：string rule= "'^\\\\d{9}$'";
    var r = exp.test(value);
    return r;
}, "请输入两位小数!");
// 两位小数     
jQuery.validator.addMethod("number2D", function (value, element) {
    var exp = /^[-]?([1-9]{1}[0-9]{0,}(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|\.[0-9]{1,2})$/;
    return this.optional(element) || (exp.test(value));
}, "请输入两位小数!");
// ipv4
jQuery.validator.addMethod("ipv4", function (value, element) {
    var exp = /^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/;
    return this.optional(element) || (exp.test(value));
}, "输入的IP地址格式不正确!");
// 手机号
jQuery.validator.addMethod("cellphone", function (value, element) {
    var exp = /^(13|15|18)[0-9]{9}$/;
    return this.optional(element) || (exp.test(value));
}, "输入的手机号码格式不正确!");
//手机和电话
jQuery.validator.addMethod("cellphoneAndTelephone", function (value, element) {
    var mobilePhone = /^(\d{11})$|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$/;
    return this.optional(element) || (mobilePhone.test(value));
}, "输入的联系方式格式不正确!");
// 身份证号
jQuery.validator.addMethod("idCard", function (value, element) {
    var exp = /(^\d{17}[\d|X]$)|(^\d{15}$)/;
    return this.optional(element) || (exp.test(value));
}, "身份证为15/18位,X必须大写!");
// 邮政编码
jQuery.validator.addMethod("zipCodeCN", function (value, element) {
    var exp = /^\d{6}$/;
    return this.optional(element) || (exp.test(value));
}, "输入的邮政编码格式不正确!");
// 图片名称格式
jQuery.validator.addMethod("picture", function (value, element) {
    var exp = /(.*)\\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$/;
    return this.optional(element) || (exp.test(value));
}, "输入的图片名称格式不正确!");
// 中文字符
jQuery.validator.addMethod("chinese", function (value, element) {
    var exp = /^[\u4e00-\u9fa5]+$/;
    return this.optional(element) || (exp.test(value));
}, "输入的内容必须是中文!");
// 数字验证
jQuery.validator.addMethod("numberFun", function (value, element, param) {
    var result = true;
    var dLen = 0;
    var isDec;
    for (i = 0; i < value.length; i++) {
        var charCode = value.charCodeAt(i);
        if (i == 0 && charCode == 45 && !param[0]) {
            result = false;
            break;
        }
        if (param[1] > 0 && !isDec && charCode == 46) {
            isDec = true;
            continue;
        }
        if (charCode < 48 || charCode > 57) {
            result = false;
            break;
        }
        else {
            if (isDec) dLen++;
            if (dLen > param[1]) result = false;
        }
    }
    return this.optional(element) || result;
}, "请输入正确的数字!");
// 数字比对<=  指定元素的值 比较指定值得Jquery有自己的如：max:8;
jQuery.validator.addMethod("beSmallerOrEqualTo", function (value, element, param) {
    var target = $(param);
    var result = Number(value) <= Number(target.val());
    return this.optional(element) || result;
}, "请输入正确的数字!");

// 数字比对大于
jQuery.validator.addMethod("greaterThan", function (value, element, param) {
    var result = Number(value) > Number(param);
    return this.optional(element) || result;
}, "请输入正确的数字!");
// 数字比对小于
jQuery.validator.addMethod("lessThan", function (value, element, param) {
    var result = Number(value) < Number(param);
    return this.optional(element) || result;
}, "请输入正确的数字!");
// 日期比对
jQuery.validator.addMethod("beDateMaxTo", function (value, element, param) {
    var target = $(param);
    var b = jQuery.validator.dateParse(value);
    var e = jQuery.validator.dateParse(target.val());
    var result = false;
    if (isNaN(b) && isNaN(e)) {
        result = true;
    }
    else if (isNaN(b) || isNaN(e)) {
        result = false;
    }
    else {
        result = (b - e) / 3600 / 1000 <= 0;
    }
    return this.optional(element) || result;
}, "请输入正确的日期!");
//requiredSpecify
jQuery.validator.addMethod("requiredSpecify", function (value, element, param) {
    // check if dependency is met
    switch (element.nodeName.toLowerCase()) {
        case 'select':
            // could be an array for select-multiple or a string, both are fine this way
            var val = $(element).val();
            return val && val.length > 0;
        case 'input':
            if (this.checkable(element))
                return this.getLength(value, element) > 0;
        default:
            return $.trim(value).length > 0 && value != param;
    }
}, "未设置！");
//日期不能大于现在
jQuery.validator.addMethod("LessThanNow", function (value, element, param) {
    var dataVal = Date.parse(value.toString().replace(/-/g, "/"));
    var now = new Date();
    return !(dataVal > now);
}, "日期不能大于现在！");
//银行卡号
jQuery.validator.addMethod("BankCard", function (value, element, param) {
    if (value.length != 0) {
        var exp = /(^\d{16}$)|(^\d{19}$)/;
        if (!exp.test(value)) {
            return false;
        }
    }
}, "银行卡号必须为16或19数字中间不能有空格!");
//百分比验证
//param ['0%','100%']
jQuery.validator.addMethod("Percent", function (value, element, param) {
    if (value.replace(',', '').length == 0)
        return true;
    var min = parseFloat(param[0]);
    var max = parseFloat(param[1]);
    var p = parseFloat(value.replace(',', ''));
    if (p >= min && p <= max) {
        return true;
    }
    return false;
}, "");
//
jQuery.validator.addMethod("YearMonthSpanFor4ComboBox", function (value, element, param) {
    var startYear = param[0].GetValue(); //Dev对象；
    var startMonth = param[1].GetValue();
    var endYear = param[2].GetValue(); //Dev对象；
    var endMonth = param[3].GetValue();
    if (startYear > endYear|| startYear != endYear) {
        return false;
    }
    if ((startYear <= endYear) && (startMonth > endMonth)) {
        return false;
    }
    return true;

}, "请选择同一年，并且要求时间合理！");
//转换分隔符
jQuery.validator.dateParse = function (val) {
    var d = Date.parse(val);
    if (isNaN(d)) d = Date.parse(val.replace('-', '/').replace('-', '/'));
    return d;
}